package com.ztools.wx.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ztools.common.model.pc.information.InformationPoint;
import com.ztools.common.model.pc.information.InformationSignIn;
import com.ztools.common.model.pc.information.WeChatMember;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.Result;
import com.ztools.common.util.common.ResultCode;
import com.ztools.wx.service.WxMemberService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.ztools.common.util.wx.RedisUtil.CACHE_SIGN_IN_INFO;
import static com.ztools.common.util.wx.RedisUtil.CACHE_WX_MEMBER_PREFIX;

/**
 * @Author: seeTools
 * @Date: 2023/5/14  14:44
 * @Version 1.0
 */
@RestController
@RequestMapping("wx/member")
public class WxMemberController {

    @Resource
    private WxMemberService wxMemberService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/info/{openid}/{type}")
    public Result getMemberInfo(@PathVariable("openid") String openid, @PathVariable("type") String type) {
        WeChatMember member;
        String redisKey = CACHE_WX_MEMBER_PREFIX + openid;
        if ("cache".equals(type)) {
            String memberCache = stringRedisTemplate.opsForValue().get(redisKey);
            if (StrUtil.isNotEmpty(memberCache)) {
                member = JSONUtil.toBean(memberCache, WeChatMember.class);
            } else {
                member = wxMemberService.getMemberInfo(openid);
            }
        } else {
            member = wxMemberService.getMemberInfo(openid);
        }
        if (member != null) {
            stringRedisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(member), 1L, TimeUnit.DAYS);
            return Result.success().data(member);
        }
        return Result.error();
    }

    @RequestMapping("/sign/{memberId}")
    public Result checkSing(@PathVariable("memberId") Long memberId) {
        // 查看今天的签到情况
        String key = CACHE_SIGN_IN_INFO + memberId;
        String redisData = stringRedisTemplate.opsForValue().get(key);
        if (redisData == null) {
            boolean b = wxMemberService.checkSing(memberId);
            if (b) {
                return Result.success(ResultCode.SIGN);
            }
            return Result.success();
        }
        return Result.success(ResultCode.SIGN);
    }

    @RequestMapping("/sign/in")
    public Result singIn(@RequestBody InformationSignIn informationSignIn) {
        if (informationSignIn == null && informationSignIn.getMemberId() == null) {
            return Result.error("参数不完整");
        }
        Date date = new Date();
        informationSignIn.setSignInDate(new Date());
        String key = CACHE_SIGN_IN_INFO + informationSignIn.getMemberId();
        // 添加数据到数据库
        boolean b = wxMemberService.signIn(informationSignIn);
        if (b) {
            long second = DateUtil.between(date, DateUtil.endOfDay(date), DateUnit.SECOND);
            stringRedisTemplate.opsForValue().set(key, "true", second, TimeUnit.SECONDS);
            return Result.success("签到成功");
        }
        return Result.error("签到失败");
    }

    @RequestMapping("/point")
    public Result getPointRecord(@RequestBody PageUtil<InformationPoint> pageUtil) {
        Page<InformationPoint> informationPointList = wxMemberService.getPointRecord(pageUtil);
        return Result.success().data(informationPointList);
    }

}
